package com.smart.campus.web.controller.attendance;

import java.util.List;

import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.AttendanceRule;
import com.smart.campus.school.service.IAttendanceRuleService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 考勤规则Controller
 * 
 * @author Zhihe.Li
 * @date 2020-10-21
 */
@Controller("attendanceRule")
@RequestMapping("/school/attendance/rule")
public class AttendanceRuleController extends BaseController
{
    private String prefix = "school/attendance/rule";

    @Autowired
    private IAttendanceRuleService attendanceRuleService;

    @RequiresPermissions("school:rule:view")
    @GetMapping()
    public String rule()
    {
        return prefix + "/rule";
    }

    /**
     * 查询考勤规则列表
     */
    @RequiresPermissions("school:rule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AttendanceRule attendanceRule)
    {
        startPage();
        attendanceRule.setSchoolId(SchoolUtil.getSchoolId());
        List<AttendanceRule> list = attendanceRuleService.selectAttendanceRuleList(attendanceRule);
        return getDataTable(list);
    }

    /**
     * 导出考勤规则列表
     */
    @RequiresPermissions("school:rule:export")
    @Log(title = "考勤规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AttendanceRule attendanceRule)
    {
        attendanceRule.setSchoolId(SchoolUtil.getSchoolId());
        List<AttendanceRule> list = attendanceRuleService.selectAttendanceRuleList(attendanceRule);
        ExcelUtil<AttendanceRule> util = new ExcelUtil<AttendanceRule>(AttendanceRule.class);
        return util.exportExcel(list, "rule");
    }

    /**
     * 新增考勤规则
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考勤规则
     */
    @RequiresPermissions("school:rule:add")
    @Log(title = "考勤规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AttendanceRule attendanceRule)
    {
        attendanceRule.setSchoolId(SchoolUtil.getSchoolId());
        attendanceRule.setStatus(1);
        return toAjax(attendanceRuleService.insertAttendanceRule(attendanceRule));
    }

    /**
     * 修改考勤规则
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AttendanceRule attendanceRule = attendanceRuleService.selectAttendanceRuleById(id);
        mmap.put("attendanceRule", attendanceRule);
        return prefix + "/edit";
    }

    /**
     * 修改保存考勤规则
     */
    @RequiresPermissions("school:rule:edit")
    @Log(title = "考勤规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AttendanceRule attendanceRule)
    {
        return toAjax(attendanceRuleService.updateAttendanceRule(attendanceRule));
    }

    /**
     * 删除考勤规则
     */
    @RequiresPermissions("school:rule:remove")
    @Log(title = "考勤规则", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(attendanceRuleService.deleteAttendanceRuleByIds(ids));
    }

    /**
     * 查询考勤规则列表
     */
    @GetMapping("/getRuleList")
    @ResponseBody
    public List<AttendanceRule> getRuleList()
    {
        AttendanceRule attendanceRule = new AttendanceRule();
        attendanceRule.setSchoolId(SchoolUtil.getSchoolId());
        attendanceRule.setStatus(1);
        List<AttendanceRule> list = attendanceRuleService.selectAttendanceRuleList(attendanceRule);
        return list;
    }
}
